package io;

import java.io.*;

/**
 * transient 关键字详解
 * @author KaiLun
 * @version 2017/3/2 10:30
 */
public class Transient_ implements Serializable {

    private static final long serialVersionUID = -4377820862232402359L;
    private String user;
    private transient String pwd;

    Transient_(String user, String pwd) {
        this.user = user;
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        if (pwd == null) {
            pwd = "序列化后我不在了";
        }
        return "user: " + user + "\n pwd: " + pwd;
    }

    public static void main(String[] args) {
        String name = "/work/public/bold/early/src/main/java/io/logInfo.out";
        try {
            Transient_ bean = new Transient_("用户", "密码");
            System.out.println(bean.toString());
            ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(name));
            o.writeObject(bean);   //写入数据
            o.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(name));
            Transient_ bean = (Transient_) in.readObject();   //读取数据
            System.out.println(bean.toString());
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
